<template>
    <div ref="paper" class="shadow"></div>
</template>

<script>
    import Vue from 'vue';
    export default {
        props: {
            value: {
                type: Object,
                default: function () {
                    return {}
                }
            },
            data28: {
                type: Array,
                default: function () {
                    return []
                }
            }
        },
        data() {
            return {
                Temp: '',
                temp: '',
                tempdata: {},
                templateData:[],
            }
        },
        watch: {
            data28:{
                handler:function(newName, oldName) {
                    
                },
                immediate: true,
                deep: true
            },
            value: {
                handler: function(n) {
                    this.temp && (this.temp.$data.model = n)
                }, deep: true
            },
            
        },
        computed: {
            model: {
                get: function () {
                    return this.value;
                },
                set: function (val) {
                    this.$emit('input', val)
                }
            },
        },

        methods: {
            Arabia_to_Chinese: function (n) {
                var fraction = ['角', '分'];
                var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
                var unit = [
                    ['元', '万', '亿'],
                    ['', '拾', '佰', '仟']
                ];
                var head = n < 0 ? '欠' : '';
                n = Math.abs(n);

                var s = '';

                for (var i = 0; i < fraction.length; i++) {
                    s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
                }
                s = s || '整';
                n = Math.floor(n);

                for (var i = 0; i < unit[0].length && n > 0; i++) {
                    var p = '';
                    for (var j = 0; j < unit[1].length && n > 0; j++) {
                        p = digit[n % 10] + unit[1][j] + p;
                        n = Math.floor(n / 10);
                    }
                    s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
                }
                return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
            },
            init: function () {
                var that = this;
                if(that.data28 == null){
                    return;
                }
                var price = Number(this.model.earnest);
            
                that.model.earnestCalc = !!price ? ((price * 10000).toFixed()) : 0;

                that.model.earnestDx = this.Arabia_to_Chinese(price * 1000*10);

                that.Temp = Vue.extend({
                    template: '<div>' + that.data28[that.model.bhmbIndex].sample + '</div>',
                    props: {
                        value: {
                            type: Object,
                            default: function() {
                                return {}
                            }
                        }
                    },
                    data: function () {
                        return {
                            earnestCalc: '',
                            dataintemp: [],

                            model: {
                                guaranteecode:"",
                                procitycode:"",
                                procityname:"",
                                dbgs:"",
                                jyzxname:"",
                                earnest:"",
                                guaranteetype:"tbbh",
                                agencytype:"dbjg",
                                guaranteemedia:"dz",
                                guaranteeformate:"",
                                ordersrc:"wxgzh",
                                kbtime:"",
                                indatenum:"",
                                shbj:"1",
                                bhgs:"",
                                bhgscode:"",
                                bhmb:"",
                                zbdlname:"",
                                tenderee:"",
                                indatenum:"",
                                amount:"",
                                vendorid:"",
                                earnestCalc:"",
                                earnestDx:"",
                            },
                        }
                    },
                    methods:{
                        setData:function(){
                            this.model = this.value;
                            var price = Number(this.model.earnest);
                            this.model.earnestCalc = !!price ? ((price * 10000).toFixed()) : 0;

                            this.model.earnestDx = that.Arabia_to_Chinese(price * 1000*10);
                        }
                    },
                    mounted: function () {
                        
                    }
                });
                //挂载组件
                that.temp = new that.Temp({
                    propsData: {
                        value: that.value
                    }
                }).$mount();

                //that.temp.$data.model = that.value;
                that.temp.setData();
                
                if(!!that.$refs.paper){
                    //清空上一个
                    that.$refs.paper.innerHTML = '';
                    //添加最新内容
                    that.$refs.paper.appendChild(that.temp.$el);

                    that.$emit('tempok', that.value);
                }
            },
        },
        mounted: function () {
            var that = this;
            //hzb跳转页面  保证金显示初始化
            
        }
    };
</script>